package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.Classes;
import com.kindergarten.pojo.Food;
import com.kindergarten.pojo.Notice;
import com.kindergarten.pojo.User;
import com.kindergarten.service.IClassesService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-26
 */
@Controller
@RequestMapping("/classes-api")
public class ClassesController {

    @Autowired
    IClassesService classesService;

    @Autowired
    IUserService iUserService;

    /**
     * 01-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "classes";
    }

    /**
     * 02-数据加载分页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public MMGridPageVoBean page(Classes classes) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();

        //老师集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().like("user_name", classes.getUserName()).eq("del", 0).eq("user_state", "2"));

        List<String> userIds = new ArrayList<String>();
        for (User user : userList) {
            String userId = user.getUserId();
            userIds.add(userId);
        }

        if (userIds.size() == 0) {
            userIds.add("-1");
        }

        QueryWrapper<Classes> queryWrapper = new QueryWrapper<Classes>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("del", 0); //删除不查询

        queryWrapper.like(classes.getName() != null && !"".equals(classes.getName()), "name", classes.getName());
        queryWrapper.in("bzr_id", userIds);

        IPage<Classes> page = classesService.page(new Page<Classes>(classes.getPage(), classes.getLimit()), queryWrapper);

        List<Classes> records = page.getRecords();
        for (Classes record : records) {
            User user = iUserService.getById(record.getBzrId());
            record.setUserName(user.getUserName());
        }

        pageVoBean.setRows(records); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toaddpage")
    public String toaddpage(Model model) {
        //老师集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("usersObject", userList);
        return "classesAdd";
    }


    /**
     * 新增
     *
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public DataResults add(Classes classes) {
        return DataResults.success(ResultCode.SUCCESS, classesService.save(classes));
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toupdatepage")
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        Classes classes = classesService.getById(id);
        //老师集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        model.addAttribute("usersObject", userList);
        model.addAttribute("classesObject", classes);
        return "classesUpdate";  //模板引擎页面
    }

    /**
     * 更新
     *
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public DataResults update(Classes classes) {
        return DataResults.success(ResultCode.SUCCESS, classesService.updateById(classes));
    }

    /**
     * 删除
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    public DataResults delete(@PathVariable("id") Integer id) {
        Classes classes = new Classes();
        classes.setId(id);
        classes.setDel(1);
        return DataResults.success(ResultCode.SUCCESS, classesService.updateById(classes));
    }

}
